package org.ko.commons.config;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * 方便加载配置信息
 * @author zengxm 2014-9-25
 */
public class PropertyConfigurer extends PropertyPlaceholderConfigurer{

	private static Map<String, String> configMap;
	
	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess,
			Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		
		if(configMap == null)
			configMap = new HashMap<String, String>();
		
		for(Object keyObj:props.keySet()){
			String key = keyObj.toString();
			configMap.put(key, props.getProperty(key));
		}
		
		System.out.println("CONFIG:"+configMap.toString());
	}
	
	public static String getProperty(String key){
		return configMap.get(key);
	}
	
	/**
	 * 获取整数类型的配置参数
	 * @method name: getInt
	 * @return type: int
	 *	@param key
	 *	@return
	 */
	public static int getInt(String key){
		return getInt(key, 0);
	}
	
	/**
	 * 
	 * @method name: getInt
	 * @return type: int
	 *	@param key
	 *	@param defaultVal		默认值
	 *	@return
	 */
	public static int getInt(String key, int defaultVal){
		try{
			return Integer.valueOf(configMap.get(key));
		}catch(Exception e){
			return defaultVal;
		}
	}
}